#include <dbg.h>

#include <cassert>
using namespace std;

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int res = 0;
        int left = 0;
        int cnt = 0;
        for (int right = 0; right < nums.size(); ++right) {
            cnt += nums[right] == 0;  // 先将0全部变成1
            while (cnt > k) {         // 变成1的个数超过k，就将最左边的1变成0
                cnt -= nums[left] == 0;
                ++left;
            }
            res = max(res, right - left + 1);
        }
        return res;
    }
};

int main() {
    Solution s;

    vector<int> nums1 = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
    vector<int> nums2 = {
        0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1};

    int k1 = 2;
    int k2 = 3;

    assert(s.longestOnes(nums1, k1) == 6);
    assert(s.longestOnes(nums2, k2) == 10);

    return 0;
}